﻿using System.Windows;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        public static bool Bold()
        {
            if (Globals.selectedEditor == null) return false;
            if (Globals.selectedEditor.SelectedExpression == null) return false;

            if (Globals.selectedEditor.SelectedExpression is Expressions.BaseText)
            {
                Expressions.BaseText bt = Globals.selectedEditor.SelectedExpression as Expressions.BaseText;

                Action action = null;

                if (bt.FontWeight == FontWeights.Normal)
                {
                    action = new Action(bt.ExID, -1, bt.GetType().Name, "FontWeight",
                        bt.FontWeight.ToString(), FontWeights.Bold.ToString());
                    bt.FontWeight = FontWeights.Bold;
                }
                else
                {
                    action = new Action(bt.ExID, -1, bt.GetType().Name, "FontWeight",
                              bt.FontWeight.ToString(), FontWeights.Normal.ToString());
                    bt.FontWeight = FontWeights.Normal;
                }

                if (action != null)
                {
                    ModifingItem mi = new ModifingItem();
                    mi.Add(action);
                    bt.Editor.Do(mi);
                }

                return true;
            }
            else if (Globals.selectedEditor.SelectedExpression is Expressions.MarkText)
            {
                Expressions.MarkText mt = Globals.selectedEditor.SelectedExpression as Expressions.MarkText;

                Action action = null;

                if (mt.FontWeight == FontWeights.Normal)
                {
                    action = new Action(mt.ExID, -1, mt.GetType().Name, "FontWeight",
                        mt.FontWeight.ToString(), FontWeights.Bold.ToString());
                    mt.FontWeight = FontWeights.Bold;
                }
                else
                {
                    action = new Action(mt.ExID, -1, mt.GetType().Name, "FontWeight",
                              mt.FontWeight.ToString(), FontWeights.Normal.ToString());
                    mt.FontWeight = FontWeights.Normal;
                }

                if (action != null)
                {
                    ModifingItem mi = new ModifingItem();
                    mi.Add(action);
                    mt.Editor.Do(mi);
                }

                return true;
            }
            else
            {
                MessageBox.Show("只有文本块或上下标式才支持设置“加粗”格式！", Globals.appName,
                   MessageBoxButton.OK, MessageBoxImage.Warning);
                return false;
            }
        }

        public static bool Italic()
        {
            if (Globals.selectedEditor == null) return false;
            if (Globals.selectedEditor.SelectedExpression == null) return false;

            if (Globals.selectedEditor.SelectedExpression is Expressions.BaseText)
            {
                Expressions.BaseText bt = Globals.selectedEditor.SelectedExpression as Expressions.BaseText;

                Action action = null;

                if (bt.FontStyle == FontStyles.Normal)
                {
                    action = new Action(bt.ExID, -1, bt.GetType().Name, "FontStyle", bt.FontStyle.ToString(),
                        FontStyles.Italic.ToString());
                    bt.FontStyle = FontStyles.Italic;
                }
                else
                {
                    action = new Action(bt.ExID, -1, bt.GetType().Name, "FontStyle", bt.FontStyle.ToString(),
                           FontStyles.Normal.ToString());
                    bt.FontStyle = FontStyles.Normal;
                }

                if (action != null)
                {
                    ModifingItem mi = new ModifingItem();
                    mi.Add(action);
                    bt.Editor.Do(mi);
                }

                return true;
            }
            else if (Globals.selectedEditor.SelectedExpression is Expressions.MarkText)
            {
                Expressions.MarkText mt = Globals.selectedEditor.SelectedExpression as Expressions.MarkText;

                Action action = null;

                if (mt.FontStyle == FontStyles.Normal)
                {
                    action = new Action(mt.ExID, -1, mt.GetType().Name, "FontStyle", mt.FontStyle.ToString(),
                        FontStyles.Italic.ToString());
                    mt.FontStyle = FontStyles.Italic;
                }
                else
                {
                    action = new Action(mt.ExID, -1, mt.GetType().Name, "FontStyle", mt.FontStyle.ToString(),
                           FontStyles.Normal.ToString());
                    mt.FontStyle = FontStyles.Normal;
                }

                if (action != null)
                {
                    ModifingItem mi = new ModifingItem();
                    mi.Add(action);
                    mt.Editor.Do(mi);
                }

                return true;
            }
            else
            {
                MessageBox.Show("只有文本块或上下标式才支持设置“倾斜”格式！", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Warning);
                return false;
            }
        }
    }
}
